##########
#Countering Authoritarian Behavior in Democracies
#Sara B. Hobolt, Moritz Osnabruegge
#This script produces Tables 2, A2-A5
#########


library(foreign)
library(nnet)
library(factorEx)

data <- read.dta("data_analysis.dta")



#####
##Table 2
#####

data <- subset(data, select=c("ID", "Q32", "Q33", "Q34", "Q35", "Q36", "Q37", "Q38", "Q39"))
data <- unique(data)

n1 <- subset(data, Q32!=97) #A politician argued that the government may ignore courts in times of crisis.
summary(n1$Q32)
sd(n1$Q32)
n2 <- subset(data, Q33!=97) #A politician argued that the government may rule without consulting Parliament in times of crisis.
summary(n2$Q33)
sd(n2$Q33)
n3 <- subset(data, Q34!=97) #A politician argued that a politician of a different party constitutes a threat to Britain.
summary(n3$Q34)
sd(n3$Q34)
n4 <- subset(data, Q35!=97) #A politician encouraged online harassment of a politician from a different party.
summary(n4$Q35)
sd(n4$Q35)
n5 <- subset(data, Q36!=97) #A politician argued that the government should exclude certain journalists from press briefings.
summary(n5$Q36)
sd(n5$Q36)
n6 <- subset(data, Q37!=97) #A politician claimed GBP 20,000 as parliamentary expenses for private purposes.
summary(n6$Q37)
sd(n6$Q37)
n7 <- subset(data, Q38!=97) #A politician had an extramarital affair with a parliamentary assistant.
summary(n7$Q38)
sd(n7$Q38)
n8 <- subset(data, Q39!=97) #A politician ignored multiple messages from constituents.
summary(n8$Q39)
sd(n8$Q39)



#####
##Table A2 and A3
#####

data <- read.dta("data_analysis.dta")


#Dimension: Gender
rob_Gender <- multinom(Gender ~ age + gender_respondent + think_conservative + high_school +  high_income, data=data)
summary(rob_Gender)

z <- summary(rob_Gender)$coefficients/summary(rob_Gender)$standard.errors
p <- (1 - pnorm(abs(z)))*2
p

#Dimension: Party
rob_Party <- multinom(Party ~ age + gender_respondent + think_conservative + high_school + high_income, data=data)
summary(rob_Party)

z <- summary(rob_Party)$coefficients/summary(rob_Party)$standard.errors
p <- (1 - pnorm(abs(z)))*2
p

#Dimension: Minister
rob_Minister <- multinom(Minister ~ age + gender_respondent + think_conservative + high_school + high_income, data=data)
summary(rob_Minister)

z <- summary(rob_Minister)$coefficients/summary(rob_Minister)$standard.errors
p <- (1 - pnorm(abs(z)))*2
p

#Dimension: Controversy
rob_Controversy <- multinom(Controversy ~ age + gender_respondent  + think_conservative + high_school + high_income, data=data)
summary(rob_Controversy)

z <- summary(rob_Controversy)$coefficients/summary(rob_Controversy)$standard.errors
p <- (1 - pnorm(abs(z)))*2
p

#Dimension: ReactionxActor
rob_ReactionxActor <- multinom(ReactionxActor ~ age + gender_respondent + think_conservative + high_school + high_income, data=data)
summary(rob_ReactionxActor)

z <- summary(rob_ReactionxActor)$coefficients/summary(rob_ReactionxActor)$standard.errors
p <- (1 - pnorm(abs(z)))*2
p

#Dimension: ReactionxAction 
rob_ReactionxAction <- multinom(ReactionxAction ~ age + gender_respondent  + think_conservative + high_school +  high_income, data=data)
summary(rob_ReactionxAction)

z <- summary(rob_ReactionxAction)$coefficients/summary(rob_ReactionxAction)$standard.errors
p <- (1 - pnorm(abs(z)))*2
p



#####
##Tables A4 and A5
#####

non_uniform_distribution <- c()
non_uniform_distribution[["Controversy"]] <- c("Had an extramarital affair"=.1,
                                               "Argued that a politician constitutes a threat"=.1,
                                               "Argued that the government may ignore courts"=.1, 
                                               "Argued that the government may rule without consulting Parliament"=.1, 
                                               "Argued that the government should exclude journalists"=.1,
                                               "Claimed GBP 20,000 as parliamentary expenses"=.1,
                                               "Encouraged online harassment"=.1,
                                               "Ignored multiple messages"=.3)
non_uniform_distribution[["ReactionxActor"]] <- c("MPs from another party"=.5, "MPs from the candidate's own party"=.5)
non_uniform_distribution[["ReactionxAction"]] <- c("did not react" =.2,"called for the candidate to be expelled"=.1, "criticized the behavior"=.3, "defended the behavior" =.3,"refused to work with the candidate"=.1)
non_uniform_distribution[["Gender"]] <- c(Male=.65, Female=.35)
non_uniform_distribution[["Party"]] <- c(Labour=.5, Conservative=.5)
non_uniform_distribution[["Minister"]] <- c(No=.7, Yes=.3)
non_uniform_distribution

data <- read.dta("data_analysis.dta")

data$ReactionxAction <- factor(data$ReactionxAction, levels=c("did not react", "called for the candidate to be expelled", "criticized the behavior", "defended the behavior", "refused to work with the candidate"))  

data2 <- subset(data, select=c("chosen", "Gender", "Party", "Minister", "Controversy", "ReactionxActor", "ReactionxAction", "pair_id", "ID"))

out1 <- model_pAMCE(formula = chosen ~ Gender + Party + Minister + Controversy +  ReactionxActor + ReactionxAction,
                   data=data2, 
                   reg=TRUE, 
                   pair=TRUE,
                   boot=600,
                   seed=1,
                   pair_id=data2$pair_id,
                   cluster_id=data2$ID,
                   target_dist=non_uniform_distribution, target_type="marginal")

summary(out1, factor_name=c("ReactionxActor"))   
summary(out1, factor_name=c("ReactionxAction"))   

